﻿Public Class Manager
    Private Sub New()
    End Sub
    Public Const CurrentOmxVersion As Short = 4

    Public Shared Function GenerateForMovie(ByVal imdbMoviePageUrl As Uri) As Entities.OpenMediaInfo.OpenMediaInfo
        Return GenerateForMovie(imdbMoviePageUrl, Nothing)
    End Function

    Public Shared Function GenerateForMovie(ByVal imdbMoviePageUrl As Uri, ByVal files() As String) As Entities.OpenMediaInfo.OpenMediaInfo
        Dim clsRet As New Entities.OpenMediaInfo.OpenMediaInfo
        clsRet.Version = CurrentOmxVersion
        clsRet.Feature = IMDB.Manager.GetMovieInfo(imdbMoviePageUrl)

        Dim clsStreamInfo As Entities.OpenMediaInfo.StreamInfo = Nothing
        If files IsNot Nothing AndAlso files.Length > 0 Then clsStreamInfo = MediaInfoDll.Manager.GetStreamInfo(files)

        If clsStreamInfo IsNot Nothing Then
            clsRet.Streams = New Generic.List(Of Entities.OpenMediaInfo.StreamInfo)
            clsRet.Streams.Add(clsStreamInfo)
        End If
        Return clsRet
    End Function

    Public Shared Function GenerateForMovie(ByVal files() As String) As Entities.OpenMediaInfo.OpenMediaInfo
        Dim clsRet As New Entities.OpenMediaInfo.OpenMediaInfo
        clsRet.Version = 2

        Dim clsStreamInfo As Entities.OpenMediaInfo.StreamInfo = MediaInfoDll.Manager.GetStreamInfo(files)
        If clsStreamInfo IsNot Nothing Then
            clsRet.Streams = New Generic.List(Of Entities.OpenMediaInfo.StreamInfo)
            clsRet.Streams.Add(clsStreamInfo)
        End If
        Return clsRet
    End Function

    Public Shared Function GetSuggestedOmxFileName(ByVal item As Entities.SharedItem, ByVal files As Generic.List(Of String)) As String
        Dim strDir As String = item.Directory
        Dim strFilename As String
        Select Case item.GroupMode
            Case Entities.GroupMode.GroupIdenticalExtensionlessFilenames
                strFilename = IO.Path.GetFileNameWithoutExtension(FindLargestFile(files)) & ".omx"
            Case Entities.GroupMode.GroupSubDirectories
                strFilename = item.Name & ".omx"
            Case Else
                strFilename = IO.Path.GetFileNameWithoutExtension(files(0)) & ".omx"
        End Select
        Return IO.Path.Combine(strDir, strFilename)
    End Function
    Private Shared Function FindLargestFile(ByVal filePaths As Generic.List(Of String)) As String
        Dim lLargest As Long = 0
        Dim strLargest As String = Nothing
        For Each strFilePath As String In filePaths
            With New IO.FileInfo(strFilePath)
                If lLargest < .Length Then
                    strLargest = strFilePath
                    lLargest = .Length
                End If
            End With
        Next
        Return strLargest
    End Function
End Class
